package org.visionlibrary.image.geomtric.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;

import org.visionlibrary.image.geomtric.model.Vector2d;


/**
 * Klasa narzedziowa zawierajaca metody pozwalajace na kopiowanie obrazu oraz
 * rysowanie na nim linii. 
 */
public class LineExtractorUtil {
	public static void drawLine(BufferedImage image, Vector2d line, int color) {
		Graphics g = image.getGraphics();
		g.setColor(new Color(color));
		g.drawLine(line.getFirst().getXAsInt(), line.getFirst().getYAsInt(), line.getLast().getXAsInt(),
				line.getLast().getYAsInt());
	}

	public static BufferedImage createEmptyImage(BufferedImage img) {
		int type = img.getType();
		if (type == 0) {
			type = 1;
		}
		BufferedImage image = new BufferedImage(img.getWidth(),
				img.getHeight(), type);
		return image;
	}
	
	public static BufferedImage copyBufferedImage(BufferedImage bi) {
		ColorModel cm = bi.getColorModel();
		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
		WritableRaster raster = bi.copyData(null);
		return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
	}
	
	public static BufferedImage copyImage(BufferedImage img) {
		BufferedImage image = createEmptyImage(img);
		Graphics g = image.getGraphics();
		g.drawImage(img, 0, 0, null);
		g.dispose();
		return image;
	}
}